package com.hdf.etdemo.viewframe;

import com.hdf.easytools.base.EasyToolsActivity;
import com.hdf.easytools.view.tabmenu.PagerSlidingTabStrip;
import com.hdf.etdemo.R;
import com.hdf.etdemo.viewframe.fragment.AFrag;
import com.hdf.etdemo.viewframe.fragment.BFrag;
import com.hdf.etdemo.viewframe.fragment.CFrag;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;

public class SlidingtabActivity extends EasyToolsActivity {
	PagerSlidingTabStrip tabs;
	ViewPager pager;
	DisplayMetrics dm;
	AFrag afrag;
	BFrag bfrag;
	CFrag cfrag;
	String[] titles = { "AA", "BB", "CC" };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		/** 设置标题显示 */
		this.TitleVisib(0, 1, 1, 0, 0);
		/** 绑定标题点击事件 */
		this.TitleClick(0, 1, 0, 0, 0);
		/** 设置标题样式ʽ */
		this.TitleStyle(null, R.drawable.et_back, "tab菜单", null, 0);
		setContentView(R.layout.activity_slidingtab);
		dm = getResources().getDisplayMetrics();
		pager = (ViewPager) findViewById(R.id.pager);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		pager.setAdapter(new MyAdapter(getSupportFragmentManager(), titles));
		tabs.setViewPager(pager);
	}
	

	public class MyAdapter extends FragmentPagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}
		
		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}
		
		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if (afrag == null) {
					afrag = new AFrag();
				}
				return afrag;
			case 1:
				if (bfrag == null) {
					bfrag = new BFrag();
				}
				return bfrag;
			case 2:
				if (cfrag == null) {
					cfrag = new CFrag();
				}
				return cfrag;
			default:
				return null;
			}
		}
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}
}
